package tw.stock;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class FileArrayAdapter extends ArrayAdapter<ItemData> {
	private ArrayList<ItemData> items;
	stockwidget m_context = null;
	public FileArrayAdapter(stockwidget stockwidget, int textViewResourceId, ArrayList<ItemData> items) {
        super(stockwidget, textViewResourceId, items);
        this.items = items;
        m_context = stockwidget;
	}


	public View getView(int position, View convertView, ViewGroup parent) {
	        View v = convertView;
	        
	        
	        if (v == null) {
	            LayoutInflater vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            v = vi.inflate(R.layout.row, null);
	        }
	        ItemData o = items.get(position);
	        boolean bShowCheckBox = false;
	        if (o != null) {
	        	
	        		ImageView img = (ImageView) v.findViewById(R.id.icon);
	        		
	        		//Bitmap bmp = drawableToBitmap();
	        		
	        		switch(o.m_nType)
	        		{
	        		case 0:
	        			img.setImageResource(R.drawable.folder_icon);
	        			break;
	        		case 1:
	        			img.setImageResource(R.drawable.document_icon);
	        			bShowCheckBox = true;
	        			break;
	        		case 2:
	        			img.setImageResource(R.drawable.video_icon);
	        			break;
	        		case 3:
	        			img.setImageResource(R.drawable.music_icon);
	        			break;
	        		case 4:
	        			img.setImageResource(R.drawable.photo_icon);
	        			break;
	        		
	        		
	        		}
	        		
	        			
	        		//img.setMaxWidth(10);
	        		//img.setMaxHeight(10);
	        		//img=(ImageView) convertView.findViewById(R.drawable.icon_folder_orange_1);
	        		
	                TextView tt = (TextView) v.findViewById(R.id.toptext);
	                CheckBox cb = (CheckBox)v.findViewById(R.id.CheckBox01);
	                
	                //cb.setText("Name: "+o.Name);
	                cb.setTag(o);
	                cb.setOnCheckedChangeListener(mCheckBoxChanged);
	                if(bShowCheckBox)
	                {
	                	cb.setVisibility(View.VISIBLE);
	                	//if(o.isFavorite)
	                	cb.setChecked(o.isFavorite);
	                	
	                }
	                else
	                	cb.setVisibility(View.INVISIBLE);
	                if (tt != null)
	                {
	                      tt.setText(o.Name);
	                }
	                //img.setVisibility(0);
	        }
	        return v;
	}
	
	private CheckBox.OnCheckedChangeListener mCheckBoxChanged
	  = new CheckBox.OnCheckedChangeListener()
	  {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) 
		{
			// TODO Auto-generated method stub
			//buttonView.getParent()
			
			CheckBox cb = (CheckBox) buttonView;
			ItemData o = (ItemData)cb.getTag();
			if(cb.isChecked() && !m_context.IsFavorite(o.ID))
			{
				m_context.AddFavorite(o.ID, o.Name);
			}
			else if(!cb.isChecked() && m_context.IsFavorite(o.ID))
			{
				m_context.RemoveFavorite(o.ID);
			} 
		}
	  };
	  

	Bitmap drawableToBitmap(Drawable drawable) {
        


		Bitmap.Config c =drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;

                Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),  c);


                Canvas canvas = new Canvas(bitmap);
                //canvas.setBitmap(bitmap);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                drawable.draw(canvas);
                return bitmap;
        }

	private LayoutInflater getSystemService(String layoutInflaterService) {
		// TODO Auto-generated method stub
		return null;
	}

}
